import tensorflow as tf
from tensorflow.keras.datasets import fashion_mnist
import numpy as np

# 加载Fashion-MNIST数据集
(_, _), (test_images, test_labels) = fashion_mnist.load_data()

# 数据预处理
test_images = test_images.reshape(-1, 28, 28, 1).astype('float32') / 255.0
test_images = tf.image.resize(test_images, [227, 227])

# 加载保存的模型
model = tf.keras.models.load_model('./AlexNet/alexnet_fashion_mnist.h5')

# 随机选择一张测试图像
random_index = np.random.randint(0, len(test_images))
test_image = test_images[random_index:random_index + 1]
true_label = test_labels[random_index]

# 进行推理
predictions = model.predict(test_image)
predicted_label = np.argmax(predictions)

# 输出结果
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
print(f"True label: {class_names[true_label]}")
print(f"Predicted label: {class_names[predicted_label]}")
